/*
test class for trigger ChangeByPDA
详细需求请查看ChangeByPDA注释，TestChangeByPDA步骤如下：
1.程序新建Survey，Question，answerOption,response.
2.新建response时会触发trigger。
3.trigger完成后，检查TIMBASURVEYS__Survey_Summary__c，TIMBASURVEYS__Question_Summary__c，TIMBASURVEYS__QuestionType__c
   是否正确。
   
huang xiaojiang 20101109
*/
@isTest
private class TestSurveyClass
{     
   //测试trigger ChangeByPDA
   static testMethod void TestChangeByPDA()
   {
        Profile p=[select  id from profile where  name='Sales Rep - Trade' limit 1];
        User u=new user(alias='hxj',email='xjhuang87@isoftstone.com',lastName='testing'
        ,languageLocaleKey='zh_CN',localeSidkey='zh_CN' ,TimeZoneSidKey='Asia/Shanghai',EmailEncodingKey='UTF-8',
        profileId=p.id,userName='xjhuang123@isoftstone.com');
              
        System.runAs(u)
        {
          //往TIMBASURVEYS__SurveyResponse__c插入记录进行测试
          //新建一个Survey
           TIMBASURVEYS__Survey__c oSurvey=new  TIMBASURVEYS__Survey__c();
           oSurvey.Name='问卷调查测试';
           oSurvey.TIMBASURVEYS__AllowAnonymousRecipients__c=true;
           oSurvey.TIMBASURVEYS__AllowMultiFill__c=true;
           oSurvey.TIMBASURVEYS__CollectedResponses__c=0;
           oSurvey.TIMBASURVEYS__FirstCollection__c=dateTime.now();
           oSurvey.TIMBASURVEYS__QuestionsPerPage__c=1;
           insert oSurvey;
           
           //对应Survey新建问题
           TIMBASURVEYS__SurveyQuestion__c oQuestion1=new TIMBASURVEYS__SurveyQuestion__c();
           oQuestion1.TIMBASURVEYS__HasChild__c=false;
           oQuestion1.TIMBASURVEYS__Question__c='问题1';
           //QT_ST是survey hard code 的问题类型，为文本类型
           oQuestion1.TIMBASURVEYS__QuestionTypeCode__c='QT_CB';
           oQuestion1.TIMBASURVEYS__Survey__c=oSurvey.id;
           insert oQuestion1;         
           TIMBASURVEYS__SurveyQuestion__c oQuestion2=new TIMBASURVEYS__SurveyQuestion__c();
           oQuestion2.TIMBASURVEYS__HasChild__c=false;
           oQuestion2.TIMBASURVEYS__Question__c='问题2';
           //QT_CB是survey hard code 的问题类型，为多行文本类型
           oQuestion2.TIMBASURVEYS__QuestionTypeCode__c='QT_CB';
           oQuestion2.TIMBASURVEYS__Survey__c=oSurvey.id;
           insert oQuestion2;
           
           //创建问题对应的answerOption
           TIMBASURVEYS__AnswerOption__c oAnswerOption1=new  TIMBASURVEYS__AnswerOption__c();
           oAnswerOption1.TIMBASURVEYS__OptionValue__c='Option';//对应问题为文本类型，直接赋值Option
           oAnswerOption1.TIMBASURVEYS__SurveyQuestion__c=oQuestion1.id;
           oAnswerOption1.TIMBASURVEYS__Position__c=1;
           oAnswerOption1.TIMBASURVEYS__ResponseType__c='Text';
           insert oAnswerOption1;
           TIMBASURVEYS__AnswerOption__c oAnswerOption2=new  TIMBASURVEYS__AnswerOption__c();
           oAnswerOption2.TIMBASURVEYS__OptionValue__c='Option';//对应问题为文本类型，直接赋值Option
           oAnswerOption2.TIMBASURVEYS__SurveyQuestion__c=oQuestion2.id;
           oAnswerOption2.TIMBASURVEYS__Position__c=1;
           oAnswerOption2.TIMBASURVEYS__ResponseType__c='Text';
           insert oAnswerOption2;
           
           //创建答案
           list<TIMBASURVEYS__SurveyResponse__c> lstRes=new   list<TIMBASURVEYS__SurveyResponse__c>();
           TIMBASURVEYS__SurveyResponse__c oRes=new TIMBASURVEYS__SurveyResponse__c();
           oRes.TIMBASURVEYS__AnswerOption__c=oAnswerOption1.id;
           oRes.isCollectedRes__c='True';//第一个为字符串True
           oRes.TIMBASURVEYS__DateTimeResponse__c=dateTime.now();
           oRes.TIMBASURVEYS__StringResponse__c='回答1';
           oRes.TIMBASURVEYS__SurveyQuestion__c=oQuestion1.id;
           lstRes.add(oRes);
           
           oRes=new TIMBASURVEYS__SurveyResponse__c();
           oRes.TIMBASURVEYS__AnswerOption__c=oAnswerOption2.id;
           oRes.isCollectedRes__c='False';//第二个为字符串False
           oRes.TIMBASURVEYS__DateTimeResponse__c=dateTime.now();
           oRes.TIMBASURVEYS__StringResponse__c='回答2';
           oRes.TIMBASURVEYS__SurveyQuestion__c=oQuestion2.id;
           lstRes.add(oRes);
           
           insert lstRes;
           
           
           
        }

   }
   
   //测试triggerLastVisitDistributionDateChangeODI 
   static testMethod void testLastVisitDistributionDateChangeODI()
   {
      
       Profile p=[select  id from profile where  name='Sales Rep - Trade' limit 1];
        User u=new user(alias='hxj1',email='xjhuang@isoftstone.com',lastName='testing'
        ,languageLocaleKey='zh_CN',localeSidkey='zh_CN' ,TimeZoneSidKey='Asia/Shanghai',EmailEncodingKey='UTF-8',
        profileId=p.id,userName='xj1huang@isoftstone.com');
        
        System.runAs(u)
        {
          
          //新建拜访计划
          Routing_Plan_And_Visit__c oRPAV=new  Routing_Plan_And_Visit__c();
          oRPAV.Visit_Date__c=date.today();
          insert oRPAV;
          
          //新建地区
          Place__c oPlace=new Place__c();
          oPlace.name='地区1';
          oPlace.CityType__c='Base City';
          oPlace.PlaceType__c='City';
          oPlace.CityCode__c='c001';
          insert oPlace;
          
          //新建店面
          Account oAcc=new   Account();
          oAcc.Name='店面1';
          oAcc.City__c=oPlace.id;
          oAcc.Channel__c='百货店';
          oAcc.Contact__c='aaa';
          oAcc.No_of_checkout__c=22;
          oAcc.ContactPhoneNumber__c='1234567456';
          insert oAcc;
                     
          //新建2条店内摆放记录，1条是计划内，一条计划外
          //计划外
           list< In_Store_Visit__c> lstISV=new  list< In_Store_Visit__c>();
           In_Store_Visit__c oISV=new  In_Store_Visit__c();
           oISV.RoutingPlanAndVisit__c=oRPAV.id;
           oISV .Start_Time__c=dateTime.now();
           oISV.End_Time__c=dateTime.now().addHours(1);
           oISV.OutletName_del__c=oAcc.id;
           lstISV.add(oISV);
         
           //计划内 
            RoutingPlan__c oRP=new RoutingPlan__c();
            oRP.RoutingPlanAndVisit__c=oRPAV.Id;
            oRP.OutletName__c=oAcc.Id;
            oRP.StartDateTime__c=dateTime.now();
            oRP.EndDateTime__c=dateTime.now().addHours(4);
            insert oRP;
           
           In_Store_Visit__c oISV1=new  In_Store_Visit__c();
           oISV1.RoutingPlanAndVisit__c=oRPAV.id;
           oISV1.Start_Time__c=dateTime.now().addHours(2);
           oISV1.End_Time__c=dateTime.now().addHours(3);
           oISV1.OutletName_del__c=oAcc.id;
           oISV1.RoutingPlanItem__c=oRP.id;
           lstISV.add(oISV1);
           
           insert lstISV;
           
           //把一条计划外的记录改成计划内的
           oISV1.RoutingPlanItem__c=null;
           update oISV1;
           //把一条计划外记录改成计划内的
            oISV.RoutingPlanItem__c=oRP.id;
            update oISV;
            
            //增加分销信息，自动触发更新 In_Store_Visit__c
            Inventory__c oInv=new   Inventory__c ( In_Store_Visit__c=oISV.id);
            oInv.Inventory__c=12;
            insert oInv;
           
        }
      
   }
   
}